package com.xsdl.builder;

import lombok.Data;
import lombok.experimental.Accessors;

public class ThreadPool {

    private Integer coreSize;
    private Integer maxSize;
    private Integer queueSize;

    // 私有构造方法,防止外部实例化
    private ThreadPool(Builder builder) {
        this.coreSize = builder.coreSize;
        this.maxSize = builder.maxSize;
        this.queueSize = builder.queueSize;
    }

    @Data
    @Accessors(chain = true)
    public static class Builder {
        private Integer coreSize;
        private Integer maxSize;
        private Integer queueSize;

        public ThreadPool build() {
            if (!validate()) {
                throw new IllegalArgumentException("没有队列长度必须传入最大线程数");
            }
            return new ThreadPool(this);
        }

        private boolean validate() {
            // 传了队列长度
            if (queueSize != null && queueSize > 0) {
                return true;
            }
            // 没有传队列长度,那么必须传最大线程数
            return maxSize != null;
        }
    }

}
